// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Water Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_WATER

// Function declarations
reserve function Water_Function103
reserve function Water_Function107
reserve function Water_Function108
reserve function Water_Function109

// Static Values
public value Water_value31 = 0;
public value Water_value32 = 0;

// Tables
private table Water_table26
	0, 0, 0, 0, 0
end table

function Water_Function103
	switch stage.waterState
	case 0
		stage.newWaterLevel = 0x6080000
		stage.waterState = 1
		break
	case 1
		if temp0 >= 7584
			stage.newWaterLevel = 0x1280000
			stage.waterState = 2
		end if
		break
	case 2
		break
	end switch
end function



function Water_Function107
	if object[+currentPlayer].value1 > 0
		object[+currentPlayer].value1--
		if object[+currentPlayer].value2 > 0
			object[+currentPlayer].value2--
		else
			CreateTempObject(TypeName[Air Bubble], 2, object[currentPlayer].xpos, object[currentPlayer].ypos)
			object[tempObjectPos].drawOrder = 4
			object[tempObjectPos].yvel = -0x8800
			object[tempObjectPos].value2 = currentPlayer
			if object[currentPlayer].direction == FLIP_NONE
				object[tempObjectPos].xpos += 0x60000
			else
				object[tempObjectPos].xpos -= 0x60000
				object[tempObjectPos].angle = 256
			end if
			object[tempObjectPos].value1 = object[tempObjectPos].xpos
			object[+currentPlayer].value2 = 512
		end if
	else
		Rand(temp0, 3)
		if temp0 == 1
			Rand(object[+currentPlayer].value2, 16)
			object[+currentPlayer].value2 += 8
		else
			object[+currentPlayer].value2 = 512
		end if
		object[+currentPlayer].value1 = 60
		CreateTempObject(TypeName[Air Bubble], 2, object[currentPlayer].xpos, object[currentPlayer].ypos)
		object[tempObjectPos].drawOrder = 4
		object[tempObjectPos].yvel = -0x8800
		object[tempObjectPos].value2 = currentPlayer
		if object[currentPlayer].direction == FLIP_NONE
			object[tempObjectPos].xpos += 0x60000
		else
			object[tempObjectPos].xpos -= 0x60000
			object[tempObjectPos].angle = 256
		end if
		object[tempObjectPos].value1 = object[tempObjectPos].xpos
	end if
end function


function Water_Function108
	CreateTempObject(TypeName[Countdown Bubble], temp0, object[currentPlayer].xpos, object[currentPlayer].ypos)
	object[tempObjectPos].drawOrder = 5
	object[tempObjectPos].yvel = -0x8800
	object[tempObjectPos].value2 = currentPlayer
	if object[currentPlayer].direction == FLIP_NONE
		object[tempObjectPos].xpos += 0x60000
	else
		object[tempObjectPos].xpos -= 0x60000
		object[tempObjectPos].angle = 256
	end if
	object[tempObjectPos].value1 = object[tempObjectPos].xpos
end function


function Water_Function109
	if currentPlayer == 0
		arrayPos0 = Water_value31
		arrayPos0 += currentPlayer
		if object[currentPlayer].type != TypeName[Death Event]
			if music.currentTrack == 6
				if object[arrayPos0].value8 == 2
					arrayPos1 = currentPlayer
					arrayPos1 += playerCount
					if object[arrayPos1].type != invincibilityType
						object[arrayPos0].value8 = 0
					end if
				end if
				if object[arrayPos0].value8 == 7
					if PlayerObject_SuperState != 1
						object[arrayPos0].value8 = 0
					end if
				end if
				PlayMusic(object[arrayPos0].value8)
			end if
			StopSfx(SfxName[DrownWarn])
		end if
	end if
end function


event ObjectMain
	temp0 = object[0].xpos
	temp0 >>= 16
	temp1 = object[0].ypos
	temp1 >>= 16
	arrayPos0 = object.entityPos
	CallFunction(Water_Function103)
	if object.ypos < stage.newWaterLevel
		object.ypos += 0x10000
	end if
	if object.ypos > stage.newWaterLevel
		object.ypos -= 0x10000
	end if
	temp0 = oscillation
	temp0 <<= 1
	Sin(stage.waterLevel, temp0)
	stage.waterLevel <<= 10
	stage.waterLevel += object.ypos
	temp7 = stage.waterLevel
	stage.waterLevel >>= 16
	currentPlayer = 0
	while currentPlayer < playerCount
		if object[currentPlayer].value25 == 0x3800
			CheckNotEqual(object[currentPlayer].type, TypeName[Debug Mode])
			temp0 = checkResult
			CheckNotEqual(object[currentPlayer].state, PlayerObject_Death)
			temp0 &= checkResult
			CheckGreater(object[currentPlayer].ypos, temp7)
			temp0 &= checkResult
			if temp0 == 1
				if object[currentPlayer].yvel > 0
					object[currentPlayer].yvel >>= 2
				else	
					object[currentPlayer].yvel >>= 1
				end if
				if object.ypos < stage.newWaterLevel
					object[currentPlayer].yvel += 0x10000
				end if
				if object[currentPlayer].state == PlayerObject_HandleTailsFlight
					StopSfx(SfxName[Flying])
					StopSfx(SfxName[Jump])
					StopSfx(SfxName[Tired])
				end if				
				if object[currentPlayer].state == PlayerObject_HandleHammerSpin
					StopSfx(SfxName[Flying])
					StopSfx(SfxName[Jump])
					StopSfx(SfxName[Tired])
				end if
				object[currentPlayer].xvel >>= 1
				object[currentPlayer].speed >>= 1
				CallFunction(PlayerObject_SwapPhysics)
				if object[currentPlayer].yvel != 0
					if object[currentPlayer].state != PlayerObject_HandleMonikaTeleport				
						CreateTempObject(TypeName[Water Splash], 0, object[currentPlayer].xpos, temp7)
						object[tempObjectPos].drawOrder = 4
						PlaySfx(SfxName[Water Splash], 0)
					end if
				end if
				object[+currentPlayer].value1 = 52
				object[currentPlayer].value3 = 0
			end if
		else
			if object[currentPlayer].value7 == 0
				if object[currentPlayer].value37 == 3
					temp2 = 0
					while temp2 < 8
						Rand(temp0, 32)
						Rand(temp1, 32)
						temp0 -= 16
						temp0 <<= 16
						temp0 += object[currentPlayer].xpos
						temp1 -= 16
						temp1 <<= 16
						temp1 += object[currentPlayer].ypos
						CreateTempObject(TypeName[Dust Puff], 0, temp0, temp1)
						object[tempObjectPos].drawOrder = object[currentPlayer].value18
						object[tempObjectPos].drawOrder++
						object[tempObjectPos].xvel = object[currentPlayer].xvel
						object[tempObjectPos].yvel = object[currentPlayer].yvel
						temp2++
					loop
					object[currentPlayer].value37 = 0
					arrayPos0 = currentPlayer
					arrayPos0 += playerCount
					CallFunction(PlayerObject_RestorePowerup)
				end if
				if object[currentPlayer].value37 == 4
					object[currentPlayer].value37 = 0
					arrayPos0 = currentPlayer
					arrayPos0 += playerCount
					CallFunction(PlayerObject_RestorePowerup)
					MBZSetup_value28 = 4
				end if
			end if
			//if object[currentPlayer].value37 != 2
			CheckEqual(object[currentPlayer].value37, 2)
			temp2 = checkResult
			CheckEqual(object[currentPlayer].value37, 5)
			temp2 |= checkResult
			if PlayerObject_SuperState == 1
				if saved.gogoplus > 0
					if options.gameMode != 2
						temp2 = 1
					end if
				end if
			end if
			
			
			if temp2 == 0
				if object[currentPlayer].value3 == 0
					object[currentPlayer].value4 = 0
					CallFunction(Water_Function109)
				end if
				switch object[currentPlayer].value4
				case 0
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 360
						if currentPlayer == 0
							PlaySfx(SfxName[Drown Alert], 0)
						end if
						object[currentPlayer].value4++
					end if
					break
				case 1
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 660
						if currentPlayer == 0
							PlaySfx(SfxName[Drown Alert], 0)
						end if
						object[currentPlayer].value4++
					end if
					break
				case 2
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 960
						if currentPlayer == 0
							PlaySfx(SfxName[Drown Alert], 0)
						end if
						object[currentPlayer].value4++
					end if
					break
				case 3
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x438
						if currentPlayer == 0
							if saved.Drowning == 0
								object.value8 = music.currentTrack
								PlayMusic(6)
							else
								PlaySfx(SfxName[DrownWarn],1)
							end if
						end if
						temp0 = 0
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 4
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x4B0
						temp0 = 1
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 5
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x528
						temp0 = 2
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 6
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x5A0
						temp0 = 3
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 7
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x618
						temp0 = 4
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 8
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x690
						temp0 = 5
						CallFunction(Water_Function108)
						object[currentPlayer].value4++
					end if
					break
				case 9
					CallFunction(Water_Function107)
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x708
						StopSfx(SfxName[DrownWarn])
						if object[currentPlayer].state != PlayerObject_Death
							if currentPlayer == 0
								stage.timeEnabled = 0
								screen.cameraEnabled = 0
							end if
							object[currentPlayer].state = PlayerObject_Drown
							object[currentPlayer].animation = ANI_DROWNING
							object[currentPlayer].speed = 0
							object[currentPlayer].xvel = 0
							object[currentPlayer].yvel = 0
							object[currentPlayer].tileCollisions = 0
							object[currentPlayer].interaction = 0
							object[currentPlayer].value18 = 5
							object[+currentPlayer].value1 = 2
							PlaySfx(SfxName[Drowning], 0)
							object[currentPlayer].value4++
						end if
					end if
					break
				case 10
					object[currentPlayer].value3++
					if object[currentPlayer].value3 == 0x744
						object[currentPlayer].value4++
					end if
					if object[+currentPlayer].value1 > 0
						object[+currentPlayer].value1--
						if object[+currentPlayer].value2 > 0
							object[+currentPlayer].value2--
						else
							Rand(temp1, 2)
							temp1 += 2
							CreateTempObject(TypeName[Air Bubble], temp1, object[currentPlayer].xpos, object[currentPlayer].ypos)
							object[tempObjectPos].drawOrder = 5
							object[tempObjectPos].yvel = -0x8800
							object[tempObjectPos].ypos -= 0x60000
							object[tempObjectPos].value2 = currentPlayer
							Rand(object[tempObjectPos].angle, 256)
							object[tempObjectPos].value1 = object[tempObjectPos].xpos
							object[+currentPlayer].value2 = 512
						end if
					else
						Rand(temp0, 5)
						if temp0 == 1
							object[+currentPlayer].value2 = 2
						else
							object[+currentPlayer].value2 = 512
						end if
						object[+currentPlayer].value1 = 6
						Rand(temp1, 2)
						temp1 += 2
						CreateTempObject(TypeName[Air Bubble], temp1, object[currentPlayer].xpos, object[currentPlayer].ypos)
						object[tempObjectPos].drawOrder = 5
						object[tempObjectPos].yvel = -0x8800
						object[tempObjectPos].ypos -= 0x60000
						object[tempObjectPos].value2 = currentPlayer
						Rand(object[tempObjectPos].angle, 256)
						object[tempObjectPos].value1 = object[tempObjectPos].xpos
					end if
					break
				end switch
			else
				if object[currentPlayer].value37 == 5
					CallFunction(Water_Function107)
				end if
				if object[currentPlayer].value3 == 0
					object[currentPlayer].value4 = 0
					CallFunction(Water_Function109)
				end if
			end if
			CheckEqual(object[currentPlayer].type, TypeName[Debug Mode])
			temp0 = checkResult
			CheckLower(object[currentPlayer].ypos, temp7)
			temp0 |= checkResult
			if temp0 == 1
				if object[currentPlayer].state != PlayerObject_Death
					object[currentPlayer].yvel <<= 1
					if object[currentPlayer].yvel < -0x100000
						object[currentPlayer].yvel = -0x100000
					end if
					
					if object[currentPlayer].state == PlayerObject_HandleTailsFlight
						if object[currentPlayer].value1 < 480
							PlaySfx(SfxName[Flying], 1)
						else
							PlaySfx(SfxName[Tired], 1)
						end if
					end if
					if object[currentPlayer].state == PlayerObject_HandleHammerSpin
						if object[currentPlayer].value1 < 180
							PlaySfx(SfxName[Flying], 1)
						end if
					end if
					CallFunction(PlayerObject_SwapPhysics)
					if object[currentPlayer].yvel != 0
						temp0 = object.ypos
						temp0 -= object[currentPlayer].ypos
						if temp0 < 0xF00000
							if object[currentPlayer].state != PlayerObject_HandleMonikaTeleport						
								CreateTempObject(TypeName[Water Splash], 0, object[currentPlayer].xpos, temp7)
								object[tempObjectPos].drawOrder = 4
								PlaySfx(SfxName[Water Splash], 0)
							end if
						end if
					end if
					object[currentPlayer].value3 = 0
					CallFunction(Water_Function109)
				end if
			end if
		end if
		currentPlayer++
	loop
	object.animationTimer++
	object.animationTimer %= 24
	object.frame = object.animationTimer
	object.frame >>= 3
end event


event ObjectDraw
	temp0 = screen.xoffset
	Sin(temp1, oscillation)
	temp1 >>= 5
	temp0 += temp1
	temp0 &= 63
	FlipSign(temp0)
	temp1 = stage.waterLevel
	temp1 -= screen.yoffset
	while temp0 < screen.xsize
		DrawSpriteScreenFX(object.frame, FX_INK, temp0, temp1)
		temp0 += 256
		DrawSpriteScreenFX(object.frame, FX_INK, temp0, temp1)
		temp0 += 256
	loop
end event


event ObjectStartup
	LoadSpriteSheet("MBZ/Objects2.gif")
	foreach (TypeName[Water], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		object[arrayPos0].drawOrder = 5
		object[arrayPos0].inkEffect = INK_ALPHA
		object[arrayPos0].alpha = 160
		stage.newWaterLevel = 0x6080000
		stage.waterState = 0
		Water_value31 = arrayPos0
		CallFunction(Water_Function103)
		stage.waterLevel = stage.newWaterLevel
		stage.waterLevel >>= 16
		object[arrayPos0].ypos = stage.newWaterLevel
	next
	if lampPostID > 31
		stage.waterState = recWaterState
		stage.waterLevel = recWaterLevel
		stage.newWaterLevel = recWaterLevel
		stage.newWaterLevel <<= 16
		arrayPos0 = Water_value31
		object[arrayPos0].ypos = stage.newWaterLevel
	else
		if credits.screen == 0
			stage.waterState = 0
		end if
	end if
	Water_value32 = 0
	//TODO: wtf? type of "256" while "ALL_ENTITIES", should this be ACTIVE_ENTITIES?
	foreach (GROUP_PLAYERS, currentPlayer, ALL_ENTITIES)
		object[currentPlayer].value3 = 0
		if object[currentPlayer].ypos > stage.newWaterLevel
			object[currentPlayer].yvel >>= 2
			object[currentPlayer].xvel >>= 1
			object[currentPlayer].speed >>= 1
			object[currentPlayer].value20 >>= 1
			object[currentPlayer].value21 >>= 1
			object[currentPlayer].value22 >>= 1
			object[currentPlayer].value23 >>= 1
			object[currentPlayer].value25 = 0x1000
			if stage.playerListPos == 2
				object[currentPlayer].value27 = 0x30000
			else
				object[currentPlayer].value27 = 0x38000
			end if
			object[currentPlayer].value28 = -0x20000
			arrayPos0 = Water_value31
			arrayPos0 += currentPlayer
			object[arrayPos0].value1 = 52
		end if
	next
	SpriteFrame(0, -8, 256, 16, 0, 129)
	SpriteFrame(0, -8, 256, 16, 0, 146)
	SpriteFrame(0, -8, 256, 16, 0, 163)
	BrokenMonitor_value25 = Water_Function109
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
